package cn.tedu.storage;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DSAutoConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public  DataSource hikariDS(){
        return new HikariDataSource();
    }

    //创建seata-at代理数据源对象
    @Bean
    @Primary //首选对象 自动注入的时候会自动的选择 首先注入的对象 当bean中存在两个同名的数据的时候，优先注入
    public  DataSource dataSource(DataSource hikariDS){
        //hikariDataSource 是被代理的对象
        return new DataSourceProxy(hikariDS);
    }
}
